'use strict';
const {getDefaultModelConfig} = require("../../utils/common");
const { formatDecimal } = require("../../utils/index");
// 供应商报价表
module.exports = app => {
    const { STRING, INTEGER, BIGINT, DECIMAL } = app.Sequelize;
    const SupplierQuote = app.model.define(
        'SupplierQuote',
        {
            id: { type: BIGINT, primaryKey: true, autoIncrement: true },
            monthly_bidding_supplier_id: { type: BIGINT, allowNull: false, comment: '月度比价供应商ID' },
            monthly_bidding_id: { type: BIGINT, allowNull: false, comment: '月度比价ID' },
            supplier_id: { type: BIGINT, allowNull: false, comment: '供应商ID' },
            dish_id: { type: BIGINT, allowNull: false, comment: '菜品ID' },
            quoted_price: { 
                type: DECIMAL(10, 2), 
                allowNull: false, 
                comment: '报价',
                get() {
                    return formatDecimal(this.getDataValue('quoted_price'));
                }
             },
            status: { type: INTEGER, defaultValue: 1, comment: '状态 1:有效 0:无效' },
            ...getDefaultModelConfig(app)
        },
        {
            tableName: 'supplier_quote',
            timestamps: true,
            createdAt: 'create_time',
            updatedAt: 'update_time'
        }
    );
    SupplierQuote.associate = function () {
        SupplierQuote.belongsTo(app.model.Business.Dish, { foreignKey: 'dish_id', targetKey: 'id', as: 'dish' });
        // 关联普通供应商
        SupplierQuote.belongsTo(app.model.Business.Supplier, { foreignKey: 'supplier_id', targetKey: 'id', as: 'supplier' });
    };
    return SupplierQuote;
}; 
